﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

namespace Dack_Web2
{
    public partial class DealDetails : System.Web.UI.Page
    {
      
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["dealID"] == null)
                    Response.Redirect("~/MainAdmin.aspx");
                string sql = "select * from Deal d, SanPham sp where d.MaSanPham = sp.MaSanPham and MaDeal=" + Request.QueryString["dealID"].ToString();
                DataTable dt = Sql_DataProvider.SQL_ExecuteTable(sql);
                DataRow dealRow = dt.Rows[0];
                Image1.ImageUrl = "/Images/SanPhamSmall/" + dealRow["HinhGioiThieu"].ToString();
                lbTenSP.Text = dealRow["TenSanPham"].ToString();
                lbGiam.Text = dealRow["Giam"].ToString();
                lbSoLuong.Text = dealRow["SoLuong"].ToString();
                lbGia.Text = dealRow["GiaSanPham"].ToString();
                float gm = int.Parse(lbGia.Text) * (100 - int.Parse(lbGiam.Text)) / 100;
                lbGiaMoi.Text = gm.ToString();
                lbBatDau.Text = string.Format("{0:dd/MM/yyyy}", dealRow["NgayBatDau"]);
                lbKetThuc.Text = string.Format("{0:dd/MM/yyyy}", dealRow["NgayKetThuc"]);
                if (dealRow["XacNhan"].ToString() == "True")
                    cbXacNhan.Checked = true;
                else
                    cbXacNhan.Checked = false;
            }
            else
            {
                float gm = int.Parse(lbGia.Text) * (100 - int.Parse(lbGiam.Text)) / 100;
                lbGiaMoi.Text = gm.ToString();
            }
        }

        protected void btnXacNhan_Click(object sender, EventArgs e)
        {
            string str = "";
            OleDbConnection cn = Sql_DataProvider.SQLConnect();
            str = "update Deal set Giam=" + lbGiam.Text + ", SoLuong=" + lbSoLuong.Text + ", XacNhan=" + cbXacNhan.Checked.ToString() + " where MaDeal=" + Request.QueryString["dealID"].ToString();
            OleDbCommand cmd = new OleDbCommand(str, cn);

            cmd.ExecuteNonQuery();

            cn.Close();
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            string str = "";
            OleDbConnection cn = Sql_DataProvider.SQLConnect();
            str = "delete from Deal where MaDeal=" + Request.QueryString["dealID"].ToString();
            OleDbCommand cmd = new OleDbCommand(str, cn);
            cmd.ExecuteNonQuery();
            cn.Close();

            Response.Redirect("MainAdmin.aspx");
        }
    }
}